package com.yixun.qfbao.service.impl;

import com.yixun.qfbao.mapper.IndexBasicMapper;
import com.yixun.qfbao.mapper.PartnerInfoMapper;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.IndexBasicDto;
import com.yixun.qfbao.model.dto.IndexRankingDto;
import com.yixun.qfbao.service.IndexBasicService;
import com.yixun.qfbao.util.MathUtil;
import com.yixun.qfbao.xf.mapper.CustomerPremisesMapper;
import com.yixun.qfbao.xf.model.enums.CommissionStatusEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
@Service("indexBasic")
public class IndexBasicServiceImpl implements IndexBasicService{
	
	@Autowired
	private PartnerInfoMapper partnerInfoMapper;
	
	@Autowired
	private CustomerPremisesMapper customerPremisesMapper;
	
	@Autowired
	private IndexBasicMapper indexBasicMapper;

	@Override
	public IndexBasicDto findIndexBasic(Long saffId) {
		IndexBasicDto basic = new IndexBasicDto();
		PartnerInfo partnerInfo = partnerInfoMapper.selectById(saffId);
		//查询经纪人数量(当前登录人所属公司的所有经纪人)
		Integer partnerInfos = partnerInfoMapper.selectByCompany(partnerInfo.getHigherid());
		basic.setBrokersNumber(partnerInfos==null?0:partnerInfos);
		Long staffId = null;
		//是否是管理员(0是 1否)
		if("1".equals(partnerInfo.getAccountType())){
			//非管理员,需添加业务员过滤.不能展示非此业务员驻守的楼盘
			staffId = partnerInfo.getId();
		}
		//成交量：该商户下，所有报备记录中，报备状态为【已结佣】的数量
		Integer volume= customerPremisesMapper.selectByCompanyId(partnerInfo.getHigherid(), CommissionStatusEnum.has_commission.getNo(), staffId);
		
		//报备数：该商户下，所有已提交的报备记录数量（即不论是何种报备状态）
		Integer reportNumber =customerPremisesMapper.selectByCompanyId(partnerInfo.getHigherid(), "", staffId);
		//已获佣金合计：该商户所有员工【已结佣】佣金之和
		BigDecimal commissionTotol = customerPremisesMapper.sumTotal(partnerInfo.getHigherid(), staffId);
		//成交率：成交量 / 报备数
		if(reportNumber>0&&volume>0){
			basic.setTurnoverRate(new BigDecimal(MathUtil.formatDigits(MathUtil.multiply(MathUtil.divide(volume, reportNumber), 100d))));
			
		}else{
			basic.setTurnoverRate(new BigDecimal("0.00"));
		}
		
		basic.setVolume(volume);
		basic.setReportNumber(reportNumber);
		basic.setCommissionTotol(commissionTotol==null?new BigDecimal("0.00"):commissionTotol);
		return basic;
	}

	@Override
	public List<IndexRankingDto> getCommissionModelPage(Long saffId,String flag,String startTime,String endTime) {
		PartnerInfo partnerInfo = partnerInfoMapper.selectById(saffId);
		Long saffCompanyId = partnerInfo.getHigherid();
		String sqlMapper = "";
		String status = null;
		if(flag.equals("1")){
			sqlMapper = "(SUM(t.commission_amount)"
					//  +	"(select SUM(c.reward) as amount from tm_customer_premises c WHERE c.staff_id = t.staff_id AND c.customer_status in (5,6,7)))"
					  + " )AS money,";
			status=CommissionStatusEnum.has_commission.getNo();
		}else if(flag.equals("2")){
			sqlMapper = "COUNT(t.mobile) AS money,";
			status=CommissionStatusEnum.has_commission.getNo();
		}else if(flag.equals("3")){
			sqlMapper="COUNT(t.staff_id) AS money,";
		}
		//是否是管理员(0是 1否)
		if("0".equals(partnerInfo.getAccountType())){
			//管理员,可查看所有人员相关内容
			saffId = null;
		}
		List<IndexRankingDto> indexCommissionList = indexRankConvert(indexBasicMapper.findCommissionList(saffCompanyId,sqlMapper,status,startTime,endTime,saffId));
		return indexCommissionList;
	}
	
	
	/**
	* 方法描述 : 排行版数据进行处理，名次并列，则下一个成绩更低的员工的名次要在原先基础上+并列人数
	* 创建者：ThinkPad 
	* 项目名称： channel-basic
	* 类名： IndexBasicServiceImpl.java
	* 版本： v1.0
	* 创建时间： 2017年1月12日 下午2:28:49
	* @param list
	* @return List<IndexRankingDto>
	 */
	public List<IndexRankingDto> indexRankConvert(List<IndexRankingDto> list){
		if(list!=null && list.size()>0){
			for (int  i=0;i<list.size()-1;i++) {
				 for  (int j=list.size()-1;j>i;j-- ){
					 String iMoney = list.get(i).getMoney();
					 String jMoney = list.get(j).getMoney();
					 if(StringUtils.isNotEmpty(iMoney) && StringUtils.isNotEmpty(jMoney)){
						 if(jMoney.equals(iMoney)){
							 list.get(j).setRenkNo(list.get(i).getRenkNo());
						 }
					 }
				 }
			}
		}
		return list;
	}
}
